package com.xiaoye.qbproject.dds;

import com.px.jni.interfaceDDS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DdsPublisherConfig {
    @Value("${dds.path}")
    public String ddlPath;
    @Value("${dds.ddsLinuxPath}")
    public String ddsLinuxPath;
    @Value("${dds.topicName}")
    public String topicName;
    @Value("${dds.domainId}")
    public Integer domainId;
    @Value("${dds.subDomainId}")
    public Integer subDomainId;

    public static long pDomain;
    public static int pHandle;
    public static long subDomain;

    @Bean
    public interfaceDDS pDDS() {


        /*******************************************************/
        try {

            /****************加载库文件**********************/
            System.load(ddlPath + "DDS-AD\\bin64\\ACE.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\TAO.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\OpenDDS_Dcps.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\OpenDDS_Rtps.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\OpenDDS_Rtps_Udp.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\OpenDDS_Tcp.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\OpenDDS_Udp.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\Messenger.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\DDS.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\API.dll");
            System.load(ddlPath + "DDS-AD\\bin64\\JavaWrap.dll");

            //linux System.load("/home/workspace/zjbproject/bin/libJavaWrap.so");
//            System.load(ddsLinuxPath);
            /****************加载库文件**********************/

        } catch (UnsatisfiedLinkError error) {
            log.error("加载库文件失败", error);
            error.printStackTrace();
        }

        interfaceDDS pDDS = new interfaceDDS();

        //linux
//        String path = ddlPath + "DDS-AD/config/rtps.ini";
        //win
        String path = ddlPath + "DDS-AD/config/rtps.ini";

        long pDomainFactory = pDDS.DE_DDS_InitDomainFactory(path);
        System.out.println("DomainFactory:" + pDomainFactory);

        /**创建域**发布和订阅者必须在同一域中才能通信*****/
        pDomain = pDDS.DE_DDS_CreateDomain(pDomainFactory, domainId);

        /**创建主题**根据主题发送相关信息*****/
        pDDS.DE_DDS_CreateTopic(pDomain, topicName);

        /********创建发布**************/
        pHandle = pDDS.DE_DDS_CreatePublisher(pDomain);

        /**创建写入器**一个主题对应一个写入器*****/
        pDDS.DE_DDS_CreateDataWriter(pDomain, pHandle, topicName);

        /*******************************************************/


        /**创建域**发布和订阅者必须在同一域中才能通信*****/
        subDomain = pDDS.DE_DDS_CreateDomain(pDomainFactory, subDomainId);

        /**创建主题**根据主题接收相关信息*****/
        pDDS.DE_DDS_CreateTopic(subDomain, topicName);

        /********创建订阅**************/
        int subHandle = pDDS.DE_DDS_CreateSubscriber(subDomain);

        /**创建读取器**一个主题对应一个读取器*****/
        pDDS.DE_DDS_CreateDataReader(subDomain, subHandle, topicName);

        /*******************************************************/
        return pDDS;
    }

}
